init.js ➔ isPathInside   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 10
c 0
b 0
f 0
rs 10
cc 1
1
const { Module } = require('module');
2
const path = require('path');
3
4
function clearRequireCache() {
5
    for (const key of Object.keys(require.cache)) {
6
        delete require.cache[key];
7
    }
8
}
9
10
function isPathInside(childPath, parentPath) {
11
    const relation = path.relative(parentPath, childPath);
12
13
    return Boolean(
14
        relation &&
15
		relation !== '..' &&
16
		!relation.startsWith(`..${path.sep}`) &&
17
		relation !== path.resolve(childPath)
18
    );
19
}
20
21
const ROOT_FOLDER = process.cwd();
22
23
function preventParentScopeModules() {
24
    const nodeModulePaths = Module._nodeModulePaths;
25
26
    Module._nodeModulePaths = function (from) {
27
        const originalPath = nodeModulePaths.call(this, from);
28
29
30
        return originalPath.filter(function (p) {
31
            return isPathInside(p, ROOT_FOLDER);
32
        });
33
    };
34
}
35
36
clearRequireCache();
37
preventParentScopeModules();
38